<?php defined('SYSPATH') OR die('No direct access allowed.');

class Club_Member_Controller extends Master_Admin_Controller 
{
		
	public function index()
	{
		$this->page(0, 0);
	}
	
	public function page($club_id=0, $pagenum=0)
    {
		$view = new View('admin/club_member');

		$this->set_title('ผู้ดูแลระบบ - สมาชิกในคลับเพลง');
    	
		$clubs = new Clubs_Model;
        // Instantiate Pagination, passing it the total number of product rows.
        $paging = new Pagination(array
		(
			'base_url'    => 'admin/club_member/ajax_page/'.$club_id.'/',
			'uri_segment' => 5,
			'total_items' => $clubs->count_club_members($club_id),
		));

        // Render the page links
        $view->pagination = $paging->render();
		$view->clubs = $clubs->loads();
        $view->members = $clubs->browse_club_members($club_id, $paging->items_per_page, $paging->sql_offset);
    	$this->template->content = $view;
		$this->template->render(TRUE);
        
    }

    public function ajax_page($club_id=0, $pagenum=0)
    {
    	if(request::is_ajax())
	    {
	    	$this->auto_render=false;
	    	$ajax = new ISS_Ajax();
	    	
	    	$clubs = new Clubs_Model;

	        // Instantiate Pagination, passing it the total number of product rows.
	        $paging = new Pagination(array
			(
				'base_url'    => 'admin/club_member/ajax_page/'.$club_id.'/',
				'uri_segment' => 5,
				'total_items' => $clubs->count_club_members($club_id),
			));
			
			$pagination = $paging->render();
	        // Render the page links
	        $members = $clubs->browse_club_members($club_id, $paging->items_per_page, $paging->sql_offset);
		
	        $content = '
				<div id="clubMember">
					<table>
					    <tr>
					        <th>สมาชิก</th>
					        <th>ประเภท</th>
		        			<th>คำสั่ง</th>
					    </tr>';
	        
	    	$i = 0;
	    	foreach ($members as $member): 
	    		$i++;
	    		$content .= (($i % 2)==0) ? '<tr class="clubListDark">' : '<tr class="clubListLight">';
				$content .= '<td>'. html::specialchars($member->prefix . ' ' . $member->firstname . ' ' . $member->lastname) . '</td>';
				$content .= '<td>'. html::specialchars($member->member_type) . '</td>';
				$content .= '<td>';
		        $content .= '	<a class="actionDel" href="' . url::base() . '/index.php/admin/club_member/del/'. $member->club_id .'/' . $member->id .'" ><img alt="ลบ" src="'. url::base() .'/media/images/delete.png"/></a>';
		        $content .= '</td>';
				$content .= '</tr>';
			endforeach;
	        $content .= '
					</table>
					';
			$content .= $pagination;
			$content .= '
				</div>	        
	        ';

	        $selector = "clubMember";
			$ajax->addViewUpdate($selector, $content);
			
			$tag = 'pagination';
			$script = '
			$(".pagination a").click(function() {
				var uri = $(this).attr("href"); 
				ISS.Ajax.Caller.request(uri, null);		
				return false;
			});
			';
			$ajax->addExecJScript($tag, $script);
			echo json_encode($ajax->getHtmlAjax());
	    }
	    else {
	    	$this->page($club_id, $pagenum);
	    }
    }
    
	public function del($club_id, $profile_id) {
    	$clubs = new Clubs_Model;
    	$clubs->delete_member($club_id, $profile_id);
		$this->page(1);	    	
    }
}
